package org.gavin.core.conf;

import com.github.benmanes.caffeine.cache.Expiry;
import org.checkerframework.checker.index.qual.NonNegative;
import org.gavin.core.domain.ExpireCache;

import java.util.concurrent.TimeUnit;

/**
 * CaffeineExpiry
 *
 * @author grl
 * @date 2024/1/26
 */
public class CacheExpiry<T extends ExpireCache> implements Expiry<String,T> {

    @Override
    public long expireAfterCreate(String key, T value, long currentTime) {
        return TimeUnit.SECONDS.toNanos(value.getExpireTime());
    }

    @Override
    public long expireAfterUpdate(String key, T value, long currentTime, @NonNegative long currentDuration) {
        return currentDuration;
    }

    @Override
    public long expireAfterRead(String key, T value, long currentTime, @NonNegative long currentDuration) {
        return currentDuration;
    }
}
